<template>
  <div class="home-container">
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse ? '64px' : '220px'" class="home-aside">
        <div class="logo" :class="{ 'collapsed-logo': isCollapse }">
          <h2 v-if="!isCollapse">智能社团管理系统</h2>
          <h2 v-else>社团</h2>
        </div>
        
        <el-menu
          :default-active="activeMenu"
          class="home-menu"
          background-color="#304156"
          text-color="#fff"
          active-text-color="#ffd04b"
          :collapse="isCollapse"
          router
        >
          <el-menu-item index="/home">
            <el-icon><icon-menu /></el-icon>
            <template #title>首页</template>
          </el-menu-item>
          
          <el-sub-menu index="club">
            <template #title>
              <el-icon><message-box /></el-icon>
              <span>社团管理</span>
            </template>
            <el-menu-item index="/clubs">社团列表</el-menu-item>
            <el-menu-item v-if=" userInfo.role === 'admin'" index="/clubs/add">添加社团</el-menu-item>
            <el-menu-item index="/club-members">社团成员</el-menu-item>
            <el-menu-item index="/activities">活动管理</el-menu-item>
            <el-menu-item index="/activity-participations">活动参与</el-menu-item>
            <el-menu-item index="/announcements">公告管理</el-menu-item>
            <el-menu-item index="/media">媒体管理</el-menu-item>
          </el-sub-menu>
          
          <!-- 管理员才显示用户管理 -->
          <el-menu-item v-if="userInfo.role === 'admin'" index="/users">
            <el-icon><user /></el-icon>
            <template #title>用户管理</template>
          </el-menu-item>
          
          <el-menu-item index="/profile">
            <el-icon><document /></el-icon>
            <template #title>个人中心</template>
          </el-menu-item>
          <el-menu-item index="/aichat">
            <el-icon><ChatLineRound /></el-icon>
            
            <template #title>AI生成活动总结</template>
          </el-menu-item>
        </el-menu>
      </el-aside>
      
      <!-- 主体内容 -->
      <el-container class="home-main-container">
        <!-- 顶部导航 -->
        <el-header class="home-header">
          <div class="header-left">
            <el-icon class="toggle-icon" @click="toggleSidebar"><fold v-if="!isCollapse" /><expand v-else /></el-icon>
            <div class="breadcrumb">
              <el-breadcrumb separator="/">
                <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
                <el-breadcrumb-item v-if="$route.meta.title !== '首页'">{{ $route.meta.title }}</el-breadcrumb-item>
              </el-breadcrumb>
            </div>
          </div>
          
          <div class="header-right">
            <el-dropdown>
              <span class="user-info">
                <el-avatar size="small" class="avatar" :src="userInfo.profilePicture">
                  {{ userInfo.realName ? userInfo.realName.substring(0, 1) : userInfo.username.substring(0, 1) }}
                </el-avatar>
                <span class="user-name">{{ userInfo.realName || userInfo.username }}</span>
                <span class="user-role">({{ formatUserRole }})</span>
                <el-icon><arrow-down /></el-icon>
              </span>
              <template #dropdown>
                <el-dropdown-menu>
                  <el-dropdown-item @click="goToProfile">
                    <el-icon><user /></el-icon>个人信息
                  </el-dropdown-item>
                  <el-dropdown-item divided @click="handleLogout">
                    <el-icon><switch-button /></el-icon>退出登录
                  </el-dropdown-item>
                </el-dropdown-menu>
              </template>
            </el-dropdown>
          </div>
        </el-header>
        
        <!-- 内容区域 - 通过router-view显示子路由内容 -->
         <!-- 使用 <router-view> 获取当前路由匹配到的组件。
通过 v-slot 解构出当前路由匹配到的组件对象 Component。
使用 <transition> 包裹动态组件 <component>，为路由切换添加过渡效果。 -->
        <el-main class="home-main">
          <router-view v-slot="{ Component }">
            <transition name="fade" mode="out-in">
              <component :is="Component" />
            </transition>
          </router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
import { ref, computed, onMounted, watch } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { ElMessageBox } from 'element-plus'
import { useUserStore } from '@/store/user'
import { 
  Menu as IconMenu, 
  MessageBox, 
  Document, 
  Menu, 
  ArrowDown, 
  User, 
  Fold, 
  Expand,
  SwitchButton,
  ChatLineRound
} from '@element-plus/icons-vue'

export default {
  name: 'Home',
  components: {
    IconMenu,
    MessageBox,
    Document,
    Menu,
    ArrowDown,
    User,
    Fold,
    Expand,
    SwitchButton,
    ChatLineRound 
  },
  setup() {
    const router = useRouter()
    const route = useRoute()
    const userStore = useUserStore()
    
    const activeMenu = computed(() => route.path)
    const isCollapse = ref(false)
    
    // 用户信息
    const userInfo = computed(() => userStore.userInfo)
    
    // 格式化用户角色
    const formatUserRole = computed(() => {
      const roleMap = {
        'student': '学生',
        'club_leader': '社团负责人',
        'admin': '学校管理员'
      }
      return roleMap[userInfo.value.role] || userInfo.value.role
    })
    
    // 格式化日期
    const formatDate = (dateString) => {
      if (!dateString) return '-'
      const date = new Date(dateString)
      return date.toLocaleDateString('zh-CN')
    }
    
    // 切换侧边栏
    const toggleSidebar = () => {
      isCollapse.value = !isCollapse.value
      // 保存侧边栏状态到localStorage
      localStorage.setItem('sidebarStatus', isCollapse.value ? '1' : '0')
    }
    
    // 跳转到个人中心
    const goToProfile = () => {
      router.push('/profile')
    }
    
    // 退出登录
    const handleLogout = async () => {
      try {
        const result = await ElMessageBox.confirm('确定要退出登录吗?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        })
        
        // 用户点击了确定按钮
        await userStore.logout() // 等待登出操作完成
        
        // 强制页面刷新并跳转到登录页
        window.location.href = '/login'
      } catch (err) {
        // 用户取消了操作，不做任何处理
      }
    }
    
    // 从localStorage获取侧边栏状态
    onMounted(() => {
      const sidebarStatus = localStorage.getItem('sidebarStatus')
      if (sidebarStatus) {
        isCollapse.value = sidebarStatus === '1'
      }
    })
    
    return {
      activeMenu,
      isCollapse,
      userInfo,
      formatUserRole,
      formatDate,
      toggleSidebar,
      goToProfile,
      handleLogout
    }
  }
}
</script>

<style scoped>
.home-container {
  height: 100vh;
}

.home-aside {
  background-color: #304156;
  color: white;
  height: 100vh;
  overflow-x: hidden;
  transition: width 0.3s;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.logo {
  height: 60px;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #263445;
  transition: all 0.3s;
}

.collapsed-logo {
  padding: 0 10px;
}

.logo h2 {
  margin: 0;
  font-size: 18px;
  font-weight: bold;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.home-menu {
  border-right: none;
  transition: width 0.3s;
}

.home-menu:not(.el-menu--collapse) {
  width: 220px;
}

.home-main-container {
  flex-direction: column;
  height: 100vh;
}

.home-header {
  background-color: #fff;
  border-bottom: 1px solid #e6e6e6;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 20px;
  box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);
}

.header-left, .header-right {
  display: flex;
  align-items: center;
}

.toggle-icon {
  font-size: 20px;
  cursor: pointer;
  margin-right: 15px;
  transition: color 0.3s;
}

.toggle-icon:hover {
  color: #409EFF;
}

.user-info {
  cursor: pointer;
  display: flex;
  align-items: center;
  padding: 5px 10px;
  border-radius: 4px;
  transition: background-color 0.3s;
}

.user-info:hover {
  background-color: #f5f7fa;
}

.avatar {
  margin-right: 8px;
  border: 2px solid #f0f0f0;
}

.user-name {
  margin-right: 5px;
  font-weight: 500;
}

.user-role {
  font-size: 12px;
  color: #909399;
}

.home-main {
  background-color: #f0f2f5;
  height: calc(100vh - 60px);
  overflow-y: auto;
  padding: 20px;
}

/* 页面过渡动画 */
.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.3s ease;
}

.fade-enter-from,
.fade-leave-to {
  opacity: 0;
}
</style> 